/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.hibernate.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author ricardo
 */
public class SessionFactoryMysql implements Serializable{
    
    
    public static Session getCurrentSession(){
        SessionFactory factory = getSessionFactoryForMysql();
            if( factory != null && !factory.isClosed()){
                return  factory.openSession();
            }
            return null;
    }
    
    private static SessionFactory getSessionFactoryForMysql() {
        SessionFactory sessionFactory = buildSessionFactory();
        if (sessionFactory != null && !sessionFactory.isClosed()) {
            return sessionFactory;
        }
        return null;
    }
    
    private static SessionFactory buildSessionFactory() {
        return new AnnotationConfiguration().configure().buildSessionFactory();
    }
}
